<template>
  <div id="main">
    <h2>学员教学计划</h2>

    <div class="head">
      <el-button v-if="this.jurisdiction === '教务主任'" type="primary" style="margin:20px" @click="handleOpen('添加计划', 1)">添加计划</el-button>
      <el-input v-model="searchTableInfo" placeholder="请输入搜索内容" style="width:240px;float: right;margin:20px;" />
    </div>
    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
        border
        fit
        highlight-current-row
      >
        <el-table-column label="序号" type="index" align="center">
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="studentName"
          label="姓名"
        />

        <el-table-column
          align="center"
          prop="studentAge"
          :formatter="formatAge"
          label="年龄"
        />

        <el-table-column
          align="center"
          prop="studentSex"
          label="性别"

          :formatter="formatSex"
        />

        <el-table-column
          align="center"
          prop="studentJoinTime"
          label="训练时长（天）"
        />

        <el-table-column
          align="center"
          prop="clazzTypeName"
          label="课程类型"
        />

        <el-table-column
          align="center"
          prop="technicalTypeName"
          label="技术类别"
        />
        <el-table-column
          align="center"
          prop="plansTeacherName"
          label="授课教师"
        />
        <el-table-column
          align="center"
          prop="PlansPeopleResponsibleTeacherId"
          label="主责老师"
        />
        <el-table-column
          align="center"
          :formatter="dateFormat"
          prop="plansTime"
          label="录入时间"
        />
        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleOpen('修改计划', 2, Scope.row.plansId)">查看</el-button>
            <!-- <el-button type="danger" icon="el-icon-delete" @click="handleDelete(Scope.row.plansId) ">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5, 10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-dialog
      title=""
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
    >
      <el-form
        ref="plans"
        :model="plans"
        label-position="top"
        :rules="rules"
      >

        <el-form-item prop="plansTeachingScheme" label="教学方案" :label-width="formLabelWidth">

          <el-input
            v-model="plans.plansTeachingScheme"
            autocomplete="off"
            maxlength="500"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 20 }"
            placeholder="请输入内容"
            @input="change($event)"
          />

        </el-form-item>

      </el-form>
      <el-button v-if="this.jurisdiction === '责任教师'||( editType == 1 && this.jurisdiction === '教务主任')" type="primary" @click="handleUpdate('plans')">修 改</el-button>
    </el-dialog>
  </div>
</template>
<script>
import { getAllplans, getAllplansByTeacher, getAllplansA, addplans, deleteplans, updateplans, getplansById, getAllplansByMedicalID, getAllplansByDirector, getAllplansByprincipal } from '@/api/plans'
import { getAllParent, selectStudent } from '@/api/child'
import { selectstaff } from '@/api/staff'
import store from '@/store/index'
import { validateChineseName, validatePhoneTwo, validateEMail, checkSpecificKey, validateCharacter, btKeyUp, isInteger } from '@/api/validata'
import moment from 'moment'
export default {
  data() {
    return {
      jurisdiction: '',
      BB: store.getters.userId,
      newplans: {
        plansAdmissionAssessment: '',
        plansId: ''
      },
      value: '',
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      // list: [],
      tableList: [],
      searchTableInfo: '',
      plans: {},
      listLoading: false,
      dialogFormVisible: false,
      editType: 1, // 1 添加 2 修改
      child: {},
      staff: {},
      formLabelWidth: '200px',
      teacher: '',
      rules: {
        plansStudentId: [
          { required: true, message: '学生不能为空', trigger: 'blur' }
        ],
        plansPeopleId: [
          { required: true, message: '执行人不能为空', trigger: 'blur' }
        ],
        plansAdmissionAssessment: [
          { required: true, message: '入学评估不能为空', trigger: 'blur' }
          // {validator:validateChineseName, trigger: ['blur', 'change']},
        ],
        plansLessonPlan: [
          { required: true, message: '课程计划不能为空', trigger: 'blur' }
          // {validator:validateChineseName, trigger: ['blur', 'change']},
        ],
        plansStageGoal: [
          { required: true, message: '阶段目标不能为空', trigger: 'blur' }
          // {validator:validateChineseName, trigger: ['blur', 'change']},
        ],
        plansTeachingPlan: [
          { required: true, message: '教学计划不能为空', trigger: 'blur' }
          //  {validator:validateChineseName, trigger: ['blur', 'change']},
        ],
        plansTime: [
          { required: true, message: '计划时间不能为空', trigger: 'blur' }

        ],
        plansTeachingScheme: [
          { required: true, message: '教学方案不能为空', trigger: 'blur' }
          // {validator:validateChineseName, trigger: ['blur', 'change']},
        ]
      }
    }
  },
  computed: {
    // 根据计算属性模糊搜索
    list() {
      const searchTableInfo = this.searchTableInfo
      if (searchTableInfo) {
        // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
        // 注意： filter() 不会对空数组进行检测。
        // 注意： filter() 不会改变原始数组。
        return this.tableList.filter(data => {
          // some() 方法用于检测数组中的元素是否满足指定条件;
          // some() 方法会依次执行数组的每个元素：
          // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
          // 如果没有满足条件的元素，则返回false。
          // 注意： some() 不会对空数组进行检测。
          // 注意： some() 不会改变原始数组。
          return Object.keys(data).some(key => {
            // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
            // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
            return (
              String(data[key])
                .toLowerCase()
                .indexOf(searchTableInfo) > -1
            )
          })
        })
      }
      return this.tableList
    }
  },
  created() {
    this.jurisdiction = store.getters.roles[0]
    this.fetchData()
    this.getinfo()
  },
  methods: {
    // 时间格式化
    dateFormat(row, column) {
      var date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD')
    },
    // 时间格式化
    formatSex: function(row, column, cellValue) {
      if (cellValue === '1') {
        return '女'
      } else if (cellValue === '0') {
        return '男'
      }
    },

    formatAge: function(row, column, cellValue) {
      if (cellValue === -1) {
        return '未填写'
      } else {
        return cellValue
      }
    },
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    getinfo: function() {
      selectStudent().then((resp) => {
        this.child = resp.obj
      })
      selectstaff().then((resp) => {
        this.staff = resp.obj
      })
    },
    handleClick(row) {

    },
    fetchData() {
      this.tableList = []
      this.listLoading = true
      if (this.jurisdiction === '教务主任') {
        getAllplans().then(data => {
          this.newplans
          for (var i = 0; i < data.obj.length; i++) {
            this.newplans.plansId = data.obj[i].plans.plansId
            this.newplans.plansPeopleName = data.obj[i].plansPeopleName
            this.newplans.plansStudentName = data.obj[i].plansStudentName
            this.newplans.plansTeacherName = data.obj[i].plansTeacherName
            this.newplans.plansTime = data.obj[i].plans.plansTime
            this.newplans.plansLessonPlan = data.obj[i].plans.plansLessonPlan
            this.newplans.studentName = data.obj[i].plansStudent.studentName
            this.newplans.studentAge = data.obj[i].plansStudent.studentAge
            this.newplans.studentSex = data.obj[i].plansStudent.studentSex
            this.newplans.studentJoinTime = data.obj[i].plansStudent.studentJoinTime
            this.newplans.studentTreatPrincipalTeacherName = data.obj[i].plansStudent.studentTreatPrincipalTeacherName
            this.newplans.plansTeacherName = data.obj[i].plansTeacherName
            this.newplans.clazzTypeName = data.obj[i].clazz.clazz_type.clazzTypeName
            this.newplans.technicalTypeName = data.obj[i].technicalTypeName
            this.newplans.PlansPeopleResponsibleTeacherId = data.obj[i].PlansPeopleResponsibleTeacherId
            this.newplans.plansTime = data.obj[i].plans.plansTime
            this.tableList.push(this.newplans)
            this.newplans = {}
          }
          this.listLoading = false
        })
      } else if (this.jurisdiction === '医疗主管') {
        getAllplansByMedicalID(this.BB).then(data => {
          this.newplans
          for (var i = 0; i < data.obj.length; i++) {
            this.newplans.plansId = data.obj[i].plans.plansId
            this.newplans.plansPeopleName = data.obj[i].plansPeopleName
            this.newplans.plansStudentName = data.obj[i].plansStudentName
            this.newplans.plansTeacherName = data.obj[i].plansTeacherName
            this.newplans.plansTime = data.obj[i].plans.plansTime
            this.newplans.plansLessonPlan = data.obj[i].plans.plansLessonPlan
            this.newplans.studentName = data.obj[i].plansStudent.studentName
            this.newplans.studentAge = data.obj[i].plansStudent.studentAge
            this.newplans.studentSex = data.obj[i].plansStudent.studentSex
            this.newplans.studentJoinTime = data.obj[i].plansStudent.studentJoinTime
            this.newplans.studentTreatPrincipalTeacherName = data.obj[i].plansStudent.studentTreatPrincipalTeacherName
            this.newplans.plansTeacherName = data.obj[i].plansTeacherName
            this.newplans.clazzTypeName = data.obj[i].clazz.clazz_type.clazzTypeName
            this.newplans.technicalTypeName = data.obj[i].technicalTypeName
            this.newplans.PlansPeopleResponsibleTeacherId = data.obj[i].PlansPeopleResponsibleTeacherId
            this.newplans.plansTime = data.obj[i].plans.plansTime

            this.tableList.push(this.newplans)
            this.newplans = {}
          }
          this.listLoading = false
        })
      } else if (this.jurisdiction === '责任教师') {
        getAllplansByprincipal(this.BB).then(data => {
          this.newplans
          for (var i = 0; i < data.obj.length; i++) {
            this.newplans.plansId = data.obj[i].plans.plansId
            this.newplans.plansPeopleName = data.obj[i].plansPeopleName
            this.newplans.plansStudentName = data.obj[i].plansStudentName
            this.newplans.plansTeacherName = data.obj[i].plansTeacherName
            this.newplans.plansTime = data.obj[i].plans.plansTime
            this.newplans.plansLessonPlan = data.obj[i].plans.plansLessonPlan
            this.newplans.studentName = data.obj[i].plansStudent.studentName
            this.newplans.studentAge = data.obj[i].plansStudent.studentAge
            this.newplans.studentSex = data.obj[i].plansStudent.studentSex
            this.newplans.studentJoinTime = data.obj[i].plansStudent.studentJoinTime
            this.newplans.studentTreatPrincipalTeacherName = data.obj[i].plansStudent.studentTreatPrincipalTeacherName
            this.newplans.plansTeacherName = data.obj[i].plansTeacherName
            this.newplans.clazzTypeName = data.obj[i].clazz.clazz_type.clazzTypeName
            this.newplans.technicalTypeName = data.obj[i].technicalTypeName
            this.newplans.PlansPeopleResponsibleTeacherId = data.obj[i].PlansPeopleResponsibleTeacherId
            this.newplans.plansTime = data.obj[i].plans.plansTime
            this.tableList.push(this.newplans)
            this.newplans = {}
          }
          this.listLoading = false
        })
      } else if (this.jurisdiction === '教学主任') {
        getAllplansA(this.BB).then(data => {
          this.newplans
          for (var i = 0; i < data.obj.length; i++) {
            this.newplans.plansId = data.obj[i].plans.plansId
            this.newplans.plansPeopleName = data.obj[i].plansPeopleName
            this.newplans.plansStudentName = data.obj[i].plansStudentName
            this.newplans.plansTeacherName = data.obj[i].plansTeacherName
            this.newplans.plansTime = data.obj[i].plans.plansTime
            this.newplans.plansLessonPlan = data.obj[i].plans.plansLessonPlan
            this.newplans.studentName = data.obj[i].plansStudent.studentName
            this.newplans.studentAge = data.obj[i].plansStudent.studentAge
            this.newplans.studentSex = data.obj[i].plansStudent.studentSex
            this.newplans.studentJoinTime = data.obj[i].plansStudent.studentJoinTime
            this.newplans.studentTreatPrincipalTeacherName = data.obj[i].plansStudent.studentTreatPrincipalTeacherName
            this.newplans.plansTeacherName = data.obj[i].plansTeacherName
            this.newplans.clazzTypeName = data.obj[i].clazz.clazz_type.clazzTypeName
            this.newplans.technicalTypeName = data.obj[i].technicalTypeName
            this.newplans.PlansPeopleResponsibleTeacherId = data.obj[i].PlansPeopleResponsibleTeacherId
            this.newplans.plansTime = data.obj[i].plans.plansTime

            this.tableList.push(this.newplans)
            this.newplans = {}
          }
          this.listLoading = false
        })
      } else if (this.jurisdiction === '教学主管') {
        getAllplansByDirector(this.BB).then(data => {
          this.newplans
          for (var i = 0; i < data.obj.length; i++) {
            this.newplans.plansId = data.obj[i].plans.plansId
            this.newplans.plansPeopleName = data.obj[i].plansPeopleName
            this.newplans.plansStudentName = data.obj[i].plansStudentName
            this.newplans.plansTeacherName = data.obj[i].plansTeacherName
            this.newplans.plansTime = data.obj[i].plans.plansTime
            this.newplans.plansLessonPlan = data.obj[i].plans.plansLessonPlan
            this.newplans.studentName = data.obj[i].plansStudent.studentName
            this.newplans.studentAge = data.obj[i].plansStudent.studentAge
            this.newplans.studentSex = data.obj[i].plansStudent.studentSex
            this.newplans.studentJoinTime = data.obj[i].plansStudent.studentJoinTime
            this.newplans.studentTreatPrincipalTeacherName = data.obj[i].plansStudent.studentTreatPrincipalTeacherName
            this.newplans.plansTeacherName = data.obj[i].plansTeacherName
            this.newplans.clazzTypeName = data.obj[i].clazz.clazz_type.clazzTypeName
            this.newplans.technicalTypeName = data.obj[i].technicalTypeName
            this.newplans.PlansPeopleResponsibleTeacherId = data.obj[i].PlansPeopleResponsibleTeacherId
            this.newplans.plansTime = data.obj[i].plans.plansTime

            this.tableList.push(this.newplans)
            this.newplans = {}
          }
          this.listLoading = false
        })
      } else if (this.jurisdiction === '责任教师') {
        getAllplansByDirector(this.BB).then(data => {
          this.newplans
          for (var i = 0; i < data.obj.length; i++) {
            this.newplans.plansId = data.obj[i].plans.plansId
            this.newplans.plansPeopleName = data.obj[i].plansPeopleName
            this.newplans.plansStudentName = data.obj[i].plansStudentName
            this.newplans.plansTeacherName = data.obj[i].plansTeacherName
            this.newplans.plansTime = data.obj[i].plans.plansTime
            this.newplans.plansLessonPlan = data.obj[i].plans.plansLessonPlan
            this.newplans.studentName = data.obj[i].plansStudent.studentName
            this.newplans.studentAge = data.obj[i].plansStudent.studentAge
            this.newplans.studentSex = data.obj[i].plansStudent.studentSex
            this.newplans.studentJoinTime = data.obj[i].plansStudent.studentJoinTime
            this.newplans.studentTreatPrincipalTeacherName = data.obj[i].plansStudent.studentTreatPrincipalTeacherName
            this.newplans.plansTeacherName = data.obj[i].plansTeacherName
            this.newplans.clazzTypeName = data.obj[i].clazz.clazz_type.clazzTypeName
            this.newplans.technicalTypeName = data.obj[i].technicalTypeName
            this.newplans.PlansPeopleResponsibleTeacherId = data.obj[i].PlansPeopleResponsibleTeacherId
            this.newplans.plansTime = data.obj[i].plans.plansTime

            this.tableList.push(this.newplans)
            this.newplans = {}
          }
          this.listLoading = false
        })
      } else {
        getAllplansByTeacher(this.BB).then(data => {
          this.newplans
          for (var i = 0; i < data.obj.length; i++) {
            this.newplans.plansId = data.obj[i].plans.plansId
            this.newplans.plansPeopleName = data.obj[i].plansPeopleName
            this.newplans.plansStudentName = data.obj[i].plansStudentName
            this.newplans.plansTeacherName = data.obj[i].plansTeacherName
            this.newplans.plansTime = data.obj[i].plans.plansTime
            this.newplans.plansLessonPlan = data.obj[i].plans.plansLessonPlan
            this.newplans.studentName = data.obj[i].plansStudent.studentName
            this.newplans.studentAge = data.obj[i].plansStudent.studentAge
            this.newplans.studentSex = data.obj[i].plansStudent.studentSex
            this.newplans.studentJoinTime = data.obj[i].plansStudent.studentJoinTime
            this.newplans.studentTreatPrincipalTeacherName = data.obj[i].plansStudent.studentTreatPrincipalTeacherName
            this.newplans.plansTeacherName = data.obj[i].plansTeacherName
            this.newplans.clazzTypeName = data.obj[i].clazz.clazz_type.clazzTypeName
            this.newplans.technicalTypeName = data.obj[i].technicalTypeName
            this.newplans.PlansPeopleResponsibleTeacherId = data.obj[i].PlansPeopleResponsibleTeacherId
            this.newplans.plansTime = data.obj[i].plans.plansTime

            this.tableList.push(this.newplans)
            this.newplans = {}
          }
          this.listLoading = false
        })
      }
    },
    handleDelete(id) {
      this.$confirm('确认删除该记录？').then(() =>
        deleteplans(id).then(data => {
          this.$notify({
            title: '操作成功',
            type: 'success'
          })
          const totalPage = Math.ceil(
            (this.tableList.length - 1) / this.pagesize
          )
          const page =
            this.currentPage > totalPage ? totalPage : this.currentPage
          this.currentPage = this.currentPage < 1 ? 1 : page
          this.fetchData()
        })
      )
    },
    handleAddUser(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          addplans(this.plans).then(resp => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.plans = {}
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },

    handleOpen(title, type, id) {
      this.plans = {}
      this.title = title
      this.editType = type
      if (type === 2) {
        getplansById(id).then((resp) => {
          this.plans = resp.obj
        })
        this.dialogFormVisible = true
      } else {
        this.dialogFormVisible = true
      }
    },
    handleClose() {
      this.dialogFormVisible = false
      this.plans = {}
    },
    handleUpdate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          updateplans(this.plans).then(resp => {
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            this.fetchData()
          }).catch(resp => {
            this.fetchData()
          })
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    }

    // getAllJurisdiction() {
    //   getAllJurisdiction().then((resp) => {
    //     this.jurisdiction = resp.obj
    //   })
    // },
  }
}
</script>

<style scoped>
#main {
  margin-left: 5px;
  margin-right: 5px;
}
#table-area {
  margin-left: 10px;
  margin-right: 10px;
}
</style>
